package t10086.Model.Messages;

import java.io.Serial;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.util.UUID;

public class Message implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L; //版本号

    private InetSocketAddress sender;//发送者
    private InetSocketAddress receiver;//接收者
    private InetSocketAddress next;//下一个给谁

    private MessageType type;//报文类型
    private String messageId;//报文唯一标识号
    private Boolean grouped;//是否分组
    private Integer offset;//偏移量
    private Integer length;//报文长
    private Integer totalLength;//报文总长
    private Object data;//报文传输的对象
    public void AutoSingleSet(){
        this.offset = 0;
        this.length = 1;
        this.totalLength = 1;
        this.messageId = UUID.randomUUID().toString();
        this.grouped = false;
    }
    public Message() {
        AutoSingleSet();
    }
    public Message(MessageType type,Object data) {
        AutoSingleSet();
        this.data = data;
        this.type = type;
    }
    public Message(InetSocketAddress sender,InetSocketAddress receiver,MessageType type,Object data) {
        AutoSingleSet();
        this.sender = sender;
        this.receiver = receiver;
        this.next = receiver;
        this.type = type;
        this.data = data;
    }
    public <T> T convertData(Class<T> clazz) {
        if (clazz.isInstance(this.data)) {// 检查 data 是否是指定类型的实例
            return clazz.cast(this.data);
        } else {
            return null;  // 类型不匹配时，返回 null
        }
    }
    public Integer getTotalLength() {
        return totalLength;
    }

    public void setTotalLength(Integer totalLength) {
        this.totalLength = totalLength;
    }

    public InetSocketAddress getSender() {
        return sender;
    }

    public void setSender(InetSocketAddress sender) {
        this.sender = sender;
    }

    public InetSocketAddress getReceiver() {
        return receiver;
    }

    public void setReceiver(InetSocketAddress receiver) {
        this.receiver = receiver;
    }

    public InetSocketAddress getNext() {
        return next;
    }

    public void setNext(InetSocketAddress next) {
        this.next = next;
    }

    public MessageType getType() {
        return type;
    }

    public void setType(MessageType type) {
        this.type = type;
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public Boolean getGrouped() {
        return grouped;
    }

    public void setGrouped(Boolean grouped) {
        this.grouped = grouped;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
    @Override
    public String toString() {
        return "Message [Type="+type+", sender=" + sender + ", receiver=" + receiver +"]";
    }

}
